package likong.bibliothek.app.navigation;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

public class ListNavigation extends Panel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private final List<Link<?>> items;

	public ListNavigation(String id) {
		super(id);
		this.items = new ArrayList<Link<?>>();
		
		ListView<Link<?>> list = new ListView<Link<?>>("repeater", items) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<Link<?>> item) {
				item.add(item.getModelObject());
			}
		};
		
		add(list);
		
	}
	
	public int getCount() {
		return items.size();
	}
	
	public ListNavigation addItem(Link<?> link) {
		if (link == null) throw new IllegalArgumentException();
		items.add(link);
		
		return this;
	}
	
	public ListNavigation addItem(IModel<String> model, Class<?> target) {
		Link link = new BookmarkablePageLink("link", target);
		link.add(new Label("label", model.getObject()));
		
		return this.addItem(link);
	}
	
	public ListNavigation addItem(String model, Class<?> target) {
		return this.addItem(new Model<String>(model), target);
	}
	
	@Override
	public boolean isVisible() {
		if (items.size() == 0) return false;
		else return super.isVisible();
	}

}
